<?php
/**
 * Created by PhpStorm.
 * User: zhangligang
 * Date: 17/5/8
 * Time: 下午8:10
 */

namespace App\Http\Controllers;


use App\Http\Presenter\CommentPresenter;
use App\Http\Repository\CommentRepository;
use App\Http\Service\CommentService;
use App\Http\Models\Comment;
use Illuminate\Http\Request as HttpRequest;

class CommentController extends BaseController
{

    public function __construct(CommentPresenter $presenter, CommentRepository $repository, CommentService $service)
    {

        $this->view = ['activeFirst' => 'comment', 'activeSecond' => 'index'];

        parent::__construct($presenter, $repository, $service);
        
    }

    public function index()
    {

        $commentList = Comment::all(['id', 'content', 'status']);

        return view('comment.index', array_merge(['commentList' => $commentList], $this->view));
    }

    public function set(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '更新状态成功';

        if (Comment::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '更新状态,请重试';
        }

        return $this->send($code, $message);
    }

    public function delete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Comment::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

}